# Hey Emacs, this is a -*- makefile -*-
#
#   Copyright (C) 2009-2013 The Paparazzi Team
#
# This file is part of paparazzi.
#
# paparazzi is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# paparazzi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with paparazzi; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#


#
# find compiler toolchain
#
include $(PAPARAZZI_SRC)/conf/Makefile.arm-linux

# Define programs and commands.

# Launch with "make Q=''" to get full command display
Q=@

OPT=3
CSTANDARD = -std=gnu99
CINCS = $(INCLUDES) -I$(PAPARAZZI_SRC)/sw/include

# Compiler flags.
CFLAGS += $(CINCS)
CFLAGS += -O$(OPT) -mfloat-abi=softfp -mtune=cortex-a8 -mfpu=vfp -march=armv7-a
CFLAGS += -fno-short-enums
# CFLAGS += -malignment-traps
CFLAGS += -Wall -Wcast-qual -Wimplicit -Wcast-align
CFLAGS += -Wpointer-arith -Wswitch
CFLAGS += -Wredundant-decls -Wreturn-type -Wshadow -Wunused 
#-Wno-unused-result
#CFLAGS += -Wa,-adhlns=$(OBJDIR)/$(notdir $(subst $(suffix $<),.lst,$<))
#CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))

CFLAGS += -lm

# flags only for C
CFLAGS += -Wstrict-prototypes -Wmissing-declarations
CFLAGS += -Wmissing-prototypes -Wnested-externs
CFLAGS += $(CSTANDARD)
CFLAGS += $($(TARGET).CFLAGS)

LDFLAGS += -lm

CXXFLAGS = -pipe -O3 -fshow-column -ffast-math -fPIC
CXXFLAGS += -g -ffunction-sections -fdata-sections
CXXFLAGS += -mfloat-abi=softfp -mtune=cortex-a8 -mfpu=vfp -march=armv7-a
CXXFLAGS += -Wall -Wextra
CXXFLAGS += $($(TARGET).CXXFLAGS)

SRC_C_OMAP  = $($(TARGET).srcs)
OBJ_C_OMAP = $(SRC_C_OMAP:%.c=$(OBJDIR)/%.o)

SRC_CPP_OMAP  = $($(TARGET).cpp_srcs)
OBJ_CPP_OMAP = $(SRC_CPP_OMAP:%.cpp=$(OBJDIR)/%.o)

all: build

build: elf

elf: $(OBJDIR)/$(TARGET).elf

# Program the device and start it.
load upload program: $(OBJDIR)/$(TARGET).elf

# If it is not the SDK version, then kill program.elf
ifneq ($(BOARD_TYPE), sdk)
	-echo "killall -9 program.elf" | telnet $(HOST)
endif

	# Kill the application
	-echo "killall -9 $(TARGET).elf" | telnet $(HOST)

		# Upload the modules and start the application
	-{ 												\
	  echo "mkdir -p $(TARGET_DIR)";						\
	} | telnet $(HOST)

	# Upload the drivers and new application
	{																								\
	  echo "binary";																				\
	  echo "put $(PAPARAZZI_SRC)/sw/ext/ardrone2_drivers/cdc-acm.ko /$(SUB_DIR)/cdc-acm.ko";		\
	  echo "put $(PAPARAZZI_SRC)/sw/ext/ardrone2_drivers/usbserial.ko /$(SUB_DIR)/usbserial.ko";	\
	  echo "put $(PAPARAZZI_SRC)/sw/ext/ardrone2_drivers/pl2303.ko /$(SUB_DIR)/pl2303.ko";			\
	  echo "put $(PAPARAZZI_SRC)/sw/ext/ardrone2_drivers/ftdi-sio.ko /$(SUB_DIR)/ftdi-sio.ko";		\
	  echo "put $(PAPARAZZI_SRC)/sw/ext/ardrone2_drivers/cp210x.ko /$(SUB_DIR)/cp210x.ko";	\
	  echo "put $(OBJDIR)/$(TARGET).elf /$(SUB_DIR)/$(TARGET).elf";									\
	  echo "quit";																					\
	} | ftp -n $(HOST)

	# Upload the modules and start the application
	-{ 												\
	  echo "cd $(TARGET_DIR)";						\
	  echo "insmod cdc-acm.ko";						\
	  echo "insmod usbserial.ko";					\
	  echo "insmod cp210x.ko";					\
	  echo "insmod pl2303.ko";						\
	  echo "insmod ftdi-sio.ko";					\
	  echo "chmod 777 $(TARGET).elf";				\
	  echo "./$(TARGET).elf > /dev/null 2>&1 &";	\
	} | telnet $(HOST)


# Link: create ELF output file from object files.
.SECONDARY : $(OBJDIR)/$(TARGET).elf
.PRECIOUS :   $(OBJ_C_OMAP) $(OBJ_CPP_OMAP)
%.elf:   $(OBJ_C_OMAP) $(OBJ_CPP_OMAP)
	@echo LD $@
	$(Q)if (expr "$($(TARGET).cpp_srcs)"); \
		  then $(CXX) $(CXXFLAGS) $(OBJ_CPP_OMAP) $(OBJ_C_OMAP) --output $@ $(LDFLAGS) $($(TARGET).LDFLAGS); \
		  else $(CC) $(CFLAGS) $(OBJ_C_OMAP) --output $@ $(LDFLAGS) $($(TARGET).LDFLAGS); fi

# Compile: create object files from C source files
$(OBJDIR)/%.o : %.c $(OBJDIR)/../Makefile.ac
	@echo CC $@
	$(Q)test -d $(dir $@) || mkdir -p $(dir $@)
	$(Q)$(CC) -c $(CFLAGS) $< -o $@

# Compile: create object files from C++ source files
$(OBJDIR)/%.o : %.cpp $(OBJDIR)/../Makefile.ac
	@echo CXX $@
	$(Q)test -d $(dir $@) || mkdir -p $(dir $@)
	$(Q)$(CXX) -c $(CXXFLAGS) $< -o $@

# Listing of phony targets.
.PHONY : all build elf clean clean_list

#
# Dependencies
#

$(OBJDIR)/.depend:
	@echo DEPEND $@
	@test -d $(OBJDIR) || mkdir -p $(OBJDIR)
	$(Q)$(CC) -MM -MG $(CFLAGS) $($(TARGET).CFLAGS) $($(TARGET).srcs) | sed 's|\([^\.]*\.o\)|$(OBJDIR)/\1|' > $@

ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),erase)
-include $(OBJDIR)/.depend
endif
endif
